#include "setwig.h"
#include "ui_setwig.h"
#include "globalval.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include "mqtt/qmqtt.h"

SetWig::SetWig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SetWig)
{
    ui->setupUi(this);
    temp = 1;
    temp2 = 1;
    temp3 = 1;



//    label->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
    ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 0);color: black;font-family: '黑体'; font-size: 18px;");
    QTimer *timer = new QTimer(this);
    connect(timer, &QTimer::timeout,this,&SetWig::updateTime);
    timer->start(1000);
    updateTime();
}

SetWig::~SetWig()
{
    delete ui;
}

void SetWig::on_pushButton_led1_clicked()
{
    if(temp == 1)
    {
        ui->pushButton_led1->setText("OFF");
        ui->pushButton_led1->setStyleSheet("background-color:rgb(248, 255, 210);color: red;font-family: '黑体'; font-size: 28px;");
        temp = 0;
        QJsonObject object;
        object["led_1"] = 1;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);

    }
    else
    {
        ui->pushButton_led1->setText("ON");
        ui->pushButton_led1->setStyleSheet("background-color:rgb(248, 255, 210);color: #00ff7f;font-family: '黑体'; font-size: 28px;");
        temp = 1;
        QJsonObject object;
        object["led_1"] = 0;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::on_pushButton_led2_clicked()
{
    if(temp2 == 1)
    {
        ui->pushButton_led2->setText("OFF");
        ui->pushButton_led2->setStyleSheet("background-color:rgb(248, 255, 210);color: red;font-family: '黑体'; font-size: 28px;");
        temp2 = 0;
        QJsonObject object;
        object["led_2"] = 1;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);

    }
    else
    {
        ui->pushButton_led2->setText("ON");
        ui->pushButton_led2->setStyleSheet("background-color:rgb(248, 255, 210);color: #00ff7f;font-family: '黑体'; font-size: 28px;");
        temp2 = 1;
        QJsonObject object;
        object["led_2"] = 0;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::on_pushButton_buzz_clicked()
{
    if(temp3 == 1)
    {
        ui->pushButton_buzz->setText("OFF");
        ui->pushButton_buzz->setStyleSheet("background-color:rgb(248, 255, 210);color: red;font-family: '黑体'; font-size: 28px;");
        temp3 = 0;
        QJsonObject object;
        object["buz"] = 1;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);

    }
    else
    {
        ui->pushButton_buzz->setText("ON");
        ui->pushButton_buzz->setStyleSheet("background-color:rgb(248, 255, 210);color: #00ff7f;font-family: '黑体'; font-size: 28px;");
        temp3 = 1;
        QJsonObject object;
        object["buz"] = 0;

        QJsonDocument doc(object);
        QByteArray json = doc.toJson();
        QMQTT::Message msg;
        msg.setPayload(json);
        msg.setTopic("/app/data/down");

        GlobalVal::mqtt->publish(msg);
    }
}

void SetWig::updateTime()
{
    ui->label->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
}
